#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "Services_QG.h"
#include "SCI_HAL_QG.h"
#include "GPS_Handler.h"
#include "SD_Card_Handler.h"
#include "Ultrasound_Handler.h"
#include "Communications_Handler.h"

uint8 u8RxBuffer[RX_BUFFER_SIZE] = { "161229.487,3723.2475,N,12158.3416,W,1,07,1.0,9.0,M,,,,0000*18" };

#pragma DATA_SEG MY_ZEROPAGE

uint8 u8RxBuffPtr = 0;
uint8 u8GPS_Checksum = 0;
uint8 u8GPS_LastRxOK = GPS_RX_FAILURE;
uint8 u8Time_Hours = 0;
uint8 u8Time_Min = 0;
uint8 u8Time_Sec = 0;

uint8 u8counter;
/*
uint8 u8LatitudeDeg = 0;
uint8 u8LatitudeMin = 0;
uint16 u16LatitudeMinDec = 0;

uint8 u8LongitudeDeg = 0;
uint8 u8LongitudeMin = 0;
uint16 u16LongitudeMinDec = 0;
*/
uint8 u8Satellites = 0;
uint8 u8PositionFix = 0;

uint16 u16GPS_Altitude = 0;
uint8 u8Quadrant = 0;

uint16 u16Course = 0;
uint8 u8Speedkm = 0; 

extern uint8 u8IsRxValid;
extern uint8 u8IIC_TxBuffer[IIC_TX_BUFFER_SIZE];
extern uint8 u8IIC_Temp;

uint8 u8SD_Status = 0;

uUN32 uUN32_latitudeController;
uUN32 uUN32_longitudeController;
uUN32 uUN32_latitude;
uUN32 uUN32_longitude;
uUN32 uUN32_temporal1;
uUN32 uUN32_temporal2;
uUN64 uUN64_temporal3;
uUN32 uUN32_distanceY;
uUN32 uUN32_distanceX;
uint16 u16GPS_distance;
uint8 u8TableLatitude;   //Must be calculated during parsing

/* Altitude is in centimeters */
union union32bits ultrasound_altitude;

void main(void) 
{        
    EnableInterrupts; /* enable interrupts */
    /* include your code here */   

    /* Initialize clock source: internal, bus = 8 MHz */
    vfnICS_Configure (); 
    vfnSystemOptionsInit();
    
    
    //vfnGPS_WatchdogInit(); // configurar el "watchdog" de la recepcion serial
    //vfnGPS_ZeroCoordInit();  //Set the default value for controller coordinates
    vfnGPS_DistanceAproximation();
    
    /* Initialize sci module, 38400 baud, rx and tx enabled */
    /* Rx interrupts enabled, rest are default */
    //vfnSCI_SetBaudRate (SCI_9600BAUD);
    //vfnSCI_Init ();
    //vfnGPS_Init ();
    //vfnSPI_Init ();
    
    //u8IsRxValid = GPS_PARSE_GGA;
    
    //for (u8counter = 0; u8counter < RX_BUFFER_SIZE; u8counter++)
    //    u8RxBuffer[u8counter] -= 0x30;
    
    //vfnGPS_ParseBuffer();
    
    //u8SD_Status = u8SD_Initialize();
    
    vfnIIC_Init();
    
    u8IIC_TxBuffer[IIC_TX_BUFFER_SIZE - 1] = 11;
         
    for(;;) 
    {
        __RESET_WATCHDOG(); /* feeds the dog */
        
    } /* loop forever */
  /* please make sure that you never leave main */
}
